#ifdef SYPX_MEMCHECK
#include "mmgr.h"
#endif

#include "WolfSeg.h"
#include "Wolf.h"

namespace SheepDefence
{
	WolfSeg::WolfSeg()
	{
	}

	WolfSeg::~WolfSeg()
	{
	}

	WolfSeg::WolfSeg(SypX::Font* f, TutorialScene* tut, SypX::Mesh* xM, SypX::Mesh* bgMesh, SypX::Mesh* cMesh, SypX::MeshInstance* xI, SypX::MeshInstance* bg, SypX::MeshInstance* c, SypX::TextBox* box, std::vector<Actor*>& act, std::vector<Instance*>& inst, DogInstance* d, std::vector<ObstacleInstance*>& obs, Graph* envir, std::vector<SheepInstance*>& shp) : tutorial(tut), oops(false), bite(false), save(false), congrat(false), Level()
	{
		font = f;
		backgroundMesh = bgMesh;
		clockMesh = cMesh;
		background = bg;
		clock = c;
		briefBox = box;
		actors = act;
		instances = inst;
		dog = d;
		obstacles = obs;
		environment = envir; 
		sheep = shp;

		delete xI;

		numSheep = 1;

		std::vector<Instance*>::iterator i = instances.begin();

		while (i != instances.end())
		{
			(*i)->updateLevel(this);
			i++;
		}

		environment->updateLevel(this);

		Actor* currActor = Actor::loadActor("Actors/wolf.act");
		actors.push_back(currActor);

		Instance* currInstance;
		currInstance = new WolfInstance(static_cast<Wolf*>(currActor), this, 80.0f, 400.0f, 6);
		wolves.push_back(static_cast<WolfInstance*>(currInstance));
		instances.push_back(currInstance);

		currInstance = new SheepInstance(static_cast<Sheep*>(actors[2]), this, 240.0f, 400.0f, 8);
		sheep.push_back(static_cast<SheepInstance*>(currInstance));
		instances.push_back(currInstance);

		briefBox->setText("Good job at getting your sheep here. Now, it should be relatively safe from the wolves that will appear from the top. Wolves, like sheep, are afraid for dogs and they will instinctively hunt the nearest sheep that is not near a dog.");
		changeState(BRIEFING);
	}

	void WolfSeg::advance(float time)
	{
		Level::advance(time);
		
		if (sheep[1]->state == DEAD && !oops)
		{
			changeState(BRIEFING);
			briefBox->setText("Oops, looks like you were unable to protect this one. Try to kill the wolf before it kills your last sheep! To do that, simply touch the wolf and your dog will automatically track it down, when you are near enough, the dog will automatically pounce on the wolf and kill it.");
			oops = true;
		}

		if (wolves[0]->state == BITTEN && wolves[0]->stateAge > 0.5f && !bite)
		{
			changeState(BRIEFING);
			briefBox->setText("Once you have pounced on a wolf, you cannot let go until it is dead. Do be careful because while you are killing a wolf, other wolves will not be intimidated by your presence!");
			
			Instance* currInstance;
			currInstance = new WolfInstance(static_cast<Wolf*>(actors[3]), this, 0.0f, 480.0f, 9);
			wolves.push_back(static_cast<WolfInstance*>(currInstance));
			instances.push_back(currInstance);

			bite = true;
		}

		if (sheep[0]->state == BITTEN && !save)
		{
			changeState(BRIEFING);
			briefBox->setText("A wolf is biting your sheep, you can save it by attacking the wolf before your sheep is dead.");
			save = true;
		}

		if (bite && wolves[1]->state == DEAD && !congrat)
		{
			changeState(BRIEFING);
				
			if (sheep[0]->state != DEAD)
				briefBox->setText("Great Job! You managed to keep your sheep alive! Keep this up and you may be the greatest canine hero ever!");
			else
				briefBox->setText("Good job with killing the wolf, too bad you couldn't save your sheep... Better luck next time!");

			congrat = true;
		}

		if (state == INPROGRESS && congrat)
			changeState(WIN);


	}

	void WolfSeg::draw()
	{
		Level::draw();
	}
}